package com.ermax.aircond.util.web.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.richfaces.component.SortOrder;

public abstract class DataTableBean {
	private String searchStr;
	private int currentIndex;
	private int page = 1;
	
	private static final String SORT_PROPERTY_PARAMETER = "sortProperty";
	protected final String SELECTION_MODE_SINGLE = "single";
	protected final String SELECTION_MODE_MULTIPLE = "multipe";	

	private Map<String, SortOrder> sortsOrders;
	private List<String> sortPriorities;
	private String selectionMode = SELECTION_MODE_SINGLE;
	private boolean multipleSorting = true;
	
	private List<SelectItem> statusOptions;
	private List<SelectItem> typeOptions;
	
	public DataTableBean() {
		sortsOrders = new HashMap<String, SortOrder>();
		sortPriorities = new ArrayList<String>();	
		statusOptions = createStatusOptions();
		typeOptions = createTypeOptions();
	}
	
	private List<SelectItem> createStatusOptions(){
		List<SelectItem> list = new ArrayList<SelectItem>();
		list.add(new SelectItem("Y", "Y"));
		list.add(new SelectItem("N", "N"));
		return list;
	}
	
	private List<SelectItem> createTypeOptions(){
		List<SelectItem> list = new ArrayList<SelectItem>();
		list.add(new SelectItem("NORMAL", "NORMAL"));
		//list.add(new SelectItem("OTHERS", "OTHERS"));
		//list.add(new SelectItem("SPECIAL", "SPECIAL"));
		return list;
	}


	public void sort() {
		String property = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(SORT_PROPERTY_PARAMETER);

		if (property != null) {
			SortOrder currentPropertySortOrder = sortsOrders.get(property);

			if (multipleSorting) {
				if (!sortPriorities.contains(property)) {
					sortPriorities.add(property);
				}
			} else {
				sortsOrders.clear();
			}

			if (currentPropertySortOrder == null || currentPropertySortOrder.equals(SortOrder.unsorted)) {
				sortsOrders.put(property, SortOrder.ascending);
			} else if (currentPropertySortOrder.equals(SortOrder.ascending)) {
				sortsOrders.put(property, SortOrder.descending);
			} else {
				sortsOrders.put(property, SortOrder.unsorted);
			}

		}
	}

	public void modeChanged(ValueChangeEvent event) {
		clear();
	}

	public void clear() {
		sortPriorities.clear();
		sortsOrders.clear();		
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public String getSearchStr() {
		return searchStr;
	}

	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}

	public Map<String, SortOrder> getSortsOrders() {
		return sortsOrders;
	}

	public void setSortsOrders(Map<String, SortOrder> sortsOrders) {
		this.sortsOrders = sortsOrders;
	}

	public List<String> getSortPriorities() {
		return sortPriorities;
	}

	public void setSortPriorities(List<String> sortPriorities) {
		this.sortPriorities = sortPriorities;
	}

	public String getSelectionMode() {
		return selectionMode;
	}

	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public boolean isMultipleSorting() {
		return multipleSorting;
	}

	public void setMultipleSorting(boolean multipleSorting) {
		this.multipleSorting = multipleSorting;
	}

	public List<SelectItem> getStatusOptions() {
		return statusOptions;
	}

	public void setStatusOptions(List<SelectItem> statusOptions) {
		this.statusOptions = statusOptions;
	}

	public List<SelectItem> getTypeOptions() {
		return typeOptions;
	}

	public void setTypeOptions(List<SelectItem> typeOptions) {
		this.typeOptions = typeOptions;
	}
}
